import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:zeus/src/widgets/cd_text_button.dart';

void main() {
  testWidgets('CDTextButton can be tapped', (WidgetTester tester) async {
    bool tapped = false;

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: CDTextButton(
            onPressed: () {
              tapped = true;
            },
            child: const Text('Tap me'),
          ),
        ),
      ),
    );

    await tester.tap(find.byType(CDTextButton));
    await tester.pump();

    expect(tapped, isTrue);
  });

  testWidgets('CDTextButton is disabled when onPressed is null', (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: CDTextButton(
            onPressed: null, // 禁用状态
            child: const Text('Disabled'),
          ),
        ),
      ),
    );

    // 验证背景颜色是否为默认的灰色
    final Ink inkWidget = tester.widget<Ink>(find.byType(Ink));
    final BoxDecoration decoration = inkWidget.decoration as BoxDecoration;
    expect(decoration.color, Colors.grey);
  });

  testWidgets('CDTextButton applies custom disabled color', (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: CDTextButton(
            onPressed: null, // 禁用状态
            style: CDTextButton.styleFrom(
              disabledBackgroundColor: Colors.red,
            ),
            child: const Text('Disabled with custom color'),
          ),
        ),
      ),
    );

    // 验证背景颜色是否为自定义的红色
    final Ink inkWidget = tester.widget<Ink>(find.byType(Ink));
    final BoxDecoration decoration = inkWidget.decoration as BoxDecoration;
    expect(decoration.color, Colors.red);
  });


  testWidgets('CDTextButton applies custom style from styleFrom', (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: CDTextButton(
            onPressed: () {},
            style: CDTextButton.styleFrom(
              backgroundColor: Colors.green,
              textStyle: const TextStyle(color: Colors.black),
            ),
            child: const Text('Styled'),
          ),
        ),
      ),
    );

    // 验证背景颜色
    final Ink inkWidget = tester.widget<Ink>(find.byType(Ink));
    final BoxDecoration decoration = inkWidget.decoration as BoxDecoration;
    expect(decoration.color, Colors.green);

    // 验证文本样式
    final Text textWidget = tester.widget<Text>(find.text('Styled'));
    expect(textWidget.style?.color, Colors.black);
  });

  testWidgets('CDTextButton displays child widget', (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: CDTextButton(
            onPressed: () {},
            child: const Row(
              children: [
                Icon(Icons.add),
                Text('Add'),
              ],
            ),
          ),
        ),
      ),
    );

    // 验证图标和文本是否都存在
    expect(find.byIcon(Icons.add), findsOneWidget);
    expect(find.text('Add'), findsOneWidget);
  });
}